package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.NewReward;
import com.applink.sell_proof.pojo.GenericAttribute;
import com.applink.sell_proof.pojo.GenericObject;
import com.applink.sell_proof.share.Parameter;
import com.applink.sell_proof.utilXMLParser.ObjPropInfo;

public class RewardDBHelper {

    //TABLE REWARD
    public static final String TABLE_NAME = "reward_table";
	
	public static final String RW_ID = "RewardID";
	public static final String RW_TYPE = "RewardType";
	public static final String RW_AMOUNT = "RewardAmount";
	public static final String RW_OBJECT = "ObjectID";
	public static final String RW_ProgramID = "ProgramID";
	public static final String RW_ProgramName = "ProgramName";
	public static final String RW_ProgramCode = "ProgramCode";
	public static final String RW_Quarter = "Quarter";
	public static final String RW_Year = "FY2014";
	
	
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ RW_ID + " integer primary key ," 
			+ RW_OBJECT + " text, " 
			+ RW_TYPE + " text ," 
			+ RW_AMOUNT + " text ,"
			+ RW_ProgramID + " text ," 
			+ RW_ProgramName + " text ," 
			+ RW_ProgramCode + " text ," 
			+ RW_Quarter 
			+ " text ,"
			+ RW_Year + " text " + ");";
	
	public static void createReward(final NewReward reward) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(RW_ID, reward.getRewardBalanceID());
				value.put(RW_OBJECT, reward.getObjectID());
				value.put(RW_TYPE, reward.getRewardType());
				value.put(RW_AMOUNT, reward.getRewardAmount());	
				value.put(RW_ProgramID, reward.getProgramID());
				value.put(RW_ProgramName, reward.getProgramName());
				value.put(RW_ProgramCode, reward.getProgramCode());
				value.put(RW_Quarter, reward.getQuarter());	
				value.put(RW_Year, reward.getYear());
				resolver.insert(EMarketingContentProvider.REWARD_CONTENT_URI,
						value);
			}

		}.start();
	}
	
	public static void clearReward(){
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.REWARD_CONTENT_URI, null, null);
	}
	
	
	public static ArrayList<NewReward> getAllReward() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.REWARD_CONTENT_URI, null, null,
				null, null);

		if (cursor != null) {
			ArrayList<NewReward> rewards = new ArrayList<NewReward>();
			while (cursor.moveToNext()) {
				NewReward reward = new NewReward();
				reward.setRewardBalanceID(cursor.getInt(cursor.getColumnIndex(RW_ID)));
				reward.setObjectID(cursor.getInt(cursor.getColumnIndex(RW_OBJECT)));
				reward.setRewardType(cursor.getString(cursor.getColumnIndex(RW_TYPE)));
				reward.setRewardAmount(cursor.getInt(cursor.getColumnIndex(RW_AMOUNT)));	
				reward.setProgramID(cursor.getInt(cursor.getColumnIndex(RW_ProgramID)));
				reward.setProgramName(cursor.getString(cursor.getColumnIndex(RW_ProgramName)));
				reward.setProgramCode(cursor.getString(cursor.getColumnIndex(RW_ProgramCode)));
				reward.setQuarter(cursor.getString(cursor.getColumnIndex(RW_Quarter)));
				reward.setYear(cursor.getString(cursor.getColumnIndex(RW_Year)));
				rewards.add(reward);
			}
			return rewards.size() > 0 ? rewards : null;
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public static ArrayList<GenericObject> getObjectBasedConfig(ArrayList<ObjPropInfo> objPropList) {
		
		if(objPropList.size() ==0){
			return null;
		}
		
		ArrayList<GenericObject> genericObjectList = new ArrayList<GenericObject>();
		  			
	        try {
	        	//whole items in objPropList must have the same om_id since they belong to an unique Generic Object
	        	//at here we fetch data of each record based on ID of each property
	        	
	        	ContentResolver resolver = EMarketing.getInstance().getApplicationContext().getContentResolver();
	        
	        	String [] queriedColumns = new String [objPropList.size()];
	        	for (int i=0;i < objPropList.size();i++)
	        		queriedColumns[i] = objPropList.get(i).getOPID();

	        	Cursor cursor = resolver.query(
	    				EMarketingContentProvider.REWARD_CONTENT_URI, queriedColumns, null,
	    				null, null);
	        	
	            if( cursor !=null) {

	            	while(cursor.moveToNext()){	
	            		
	            		//create new genericObject
	            		GenericObject genericObject = new GenericObject();
	            		genericObject.setObjectName(objPropList.get(0).getOm());
	            		
	            		ArrayList<GenericAttribute> attributeList = new ArrayList<GenericAttribute>();
                		
	            		for(int j=0; j < objPropList.size() ; j++){ //the number of attributes of the record must be the same with objPropList's size
	                		
	            			ObjPropInfo objPropInfo = objPropList.get(j);
	                		
	                		String name = objPropInfo.getOPID();
	                		String dataType = objPropInfo.getDataType();
	                			                		
	                		if(dataType.compareTo(Parameter.TYPE_INT) == 0) {
	                			GenericAttribute<String, Integer> attribute = new GenericAttribute<String, Integer>(name, (int) cursor.getLong(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_CHAR) == 0){
	                			GenericAttribute<String, String> attribute = new GenericAttribute<String, String>(name, cursor.getString(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_DOUBLE) == 0){
	                			GenericAttribute<String, Double> attribute = new GenericAttribute<String, Double>(name, (double)cursor.getDouble(cursor.getColumnIndex(name)));           			
	                			attributeList.add(attribute);
	                		}
	                		//if you have more datatype, please define here
	                	}
	            		
	            		genericObject.setAttributeList(attributeList);
	            		genericObjectList.add(genericObject);	                

	            	}
	            }
	        } catch (SQLException e) {
	        	genericObjectList = null;
	        }
   	
    	return genericObjectList;
	}
	
	
/*	public static void updateReward(NewReward reward) {
//		ContentValues value = new ContentValues();
//		ContentResolver resolver = EMarketing.getInstance()
//				.getApplicationContext().getContentResolver();
//		value.put(RW_TYPE,reward.getRewardType());
//		value.put(RW_AMOUNT, reward.getRewardAmount());
//		value.put(RW_OBJECT, reward.getObjectID());
//		value.put(RW_NAME, reward.getProgramName());
//
//		resolver.update(EMarketingContentProvider.PROMOTION_CONTENT_URI, value,
//				PM_ID + "=?", new String[] { Long.toString(promotion.getAnnouncePromoID()) });
//
	}*/
}
